package net.zjitc.dao.Impl;

import net.zjitc.dao.CartDao;
import net.zjitc.domain.CartItem;
import net.zjitc.domain.Product;
import net.zjitc.domain.User;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.MapHandler;
import org.apache.commons.dbutils.handlers.MapListHandler;
import utils.dbutils.C3P0utils;

import java.lang.reflect.InvocationTargetException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2017/12/11.
 */
public class CartDaoImpl implements CartDao {
     QueryRunner runner=new QueryRunner(C3P0utils.getDs());
    public List<CartItem> findByUid(int uid) {
        String sql="select * from product p,cartitem c where p.pid=c.pid and uid =? order by c.orderby";
        List<CartItem> cartItemList=new ArrayList<CartItem>();
        try {
            List<Map<String,Object>> list=runner.query(sql, new MapListHandler(),uid);
            for (Map<String,Object>m:list
                 ) {
                Product product=new Product();
                CartItem cartItem=new CartItem();
                BeanUtils.populate(product,m);
                BeanUtils.populate(cartItem,m);
                cartItem.setProduct(product);
                cartItemList.add(cartItem);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } if(cartItemList.size()>0){
        return cartItemList;
        }else
        {
            return null;
        }
    }

    public CartItem findByPidAndUid(int pid, int uid) {
        String sql = "select * from cartitem where pid=? and uid=?";
        try {
            Map<String, Object> map = runner.query(sql, new MapHandler(), pid, uid);
            if (map == null) {
                return null;
            }
            CartItem cartItem = new CartItem();
            Product product = new Product();
            User user = new User();
            BeanUtils.populate(cartItem, map);
            BeanUtils.populate(product, map);
            BeanUtils.populate(user, map);
            cartItem.setProduct(product);
            cartItem.setUser(user);
            return cartItem;
        } catch (Exception e) {
            e.printStackTrace();
        }
    return null;
    }

    public int add(CartItem cartItem) {
        String sql="insert into cartitem(cartid,quantity,pid,uid) value(?,?,?,?) ";
        try {
            int count=runner.update(sql,cartItem.getCartid(),cartItem.getQuantity(),cartItem.getProduct().getPid(),cartItem.getUser().getUid());
            return count;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }

    public int update(CartItem cartItem1) {
        String sql="update cartitem set quantity=? where cartid=?";
        try {
            int count=runner.update(sql,cartItem1.getQuantity(),cartItem1.getCartid());
            return count;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }

    public int delete(int pid) {
        QueryRunner queryRunner=new QueryRunner(C3P0utils.getDs());
        String sql="delete from cartitem where pid=?";
        int num=0;
        try {
            num= queryRunner.update(sql,pid);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return num;
    }

    public int clear() {
        QueryRunner queryRunner=new QueryRunner(C3P0utils.getDs());
        String sql="delete from cartitem";
        int num=0;
        try {
            num= queryRunner.update(sql);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return num;
    }


}
